package com.woniu.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.Utiles.IsNumber;
import com.woniu.pojo.Houses;
import com.woniu.pojo.Presellapply;
import com.woniu.service.HousesService;
@RequestMapping("/houses")
@Controller
public class HousesController {
	@Autowired
	private HousesService housesService;
	@RequestMapping("/list")
	public String showHouses(Model model,HttpServletRequest req) {
		req.getSession().setAttribute("condition", "");
		PageHelper.startPage(1, 5);
		List<Houses> list = housesService.getAllHouses();
		PageInfo<Houses> pageInfo = new PageInfo<Houses>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "/commodityHourse/showlist.jsp";
	}
	@RequestMapping("/page")
	public String selectPage(Integer current,Model model,HttpServletRequest req) {
		String condition ="";
		if(!"".equals(req.getSession().getAttribute("condition"))) {
			condition=(String) req.getSession().getAttribute("condition");
		}
		PageHelper.startPage(current, 5);
		List<Houses> list = housesService.getAllHouseByCondition(condition);
		PageInfo<Houses> pageInfo = new PageInfo<Houses>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "/commodityHourse/showlist.jsp";
	}
	@RequestMapping("/next")
	public String nextPage(Integer current,Model model,HttpServletRequest req) {
		String condition ="";
		if(!"".equals(req.getSession().getAttribute("condition"))) {
			condition=(String) req.getSession().getAttribute("condition");
		}
		PageHelper.startPage(current, 5);
		List<Houses> list = housesService.getAllHouseByCondition(condition);
		PageInfo<Houses> pageInfo = new PageInfo<Houses>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "/commodityHourse/showlist.jsp";
	}
	@RequestMapping("/pre")
	public String prePage(Integer current,Model model,HttpServletRequest req) {
		String condition ="";
		if(!"".equals(req.getSession().getAttribute("condition"))) {
			condition=(String) req.getSession().getAttribute("condition");
		}
		PageHelper.startPage(current, 5);
		List<Houses> list = housesService.getAllHouseByCondition(condition);
		PageInfo<Houses> pageInfo = new PageInfo<Houses>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "/commodityHourse/showlist.jsp";
	}
	@RequestMapping("/condition")
	public String condition(String condition,HttpServletRequest req,Model model) {
		req.getSession().setAttribute("condition", condition);
		System.out.println(condition);
		PageHelper.startPage(1, 5);
		List<Houses> list = housesService.getAllHouseByCondition(condition);
		PageInfo<Houses> pageInfo = new PageInfo<Houses>(list);
		model.addAttribute("pageInfo", pageInfo);
		return "/commodityHourse/showlist.jsp";
	}
	@RequestMapping("/topresell")
	public String toPreSell() {
		return"/commodityHourse/persell.jsp";
	}
	@RequestMapping("/presell")
	public String presell() {
		return "/commodityHourse/persell.jsp";
	}
	@RequestMapping("/checkid")
	@ResponseBody
	public String checkid(String id) {
		if(IsNumber.isNumber(id)&&!"".equals(id)) {
			return"true"; 
		}else {
			return "false";
		}
	}
	@RequestMapping("/checktele")
	@ResponseBody
	public String checktele(String tele) {
		if(IsNumber.isNumber(tele)&&!"".equals(tele)) {
			return"true"; 
		}else {
			return "false";
		}
	}
	@RequestMapping("/presubmit")
	@ResponseBody
	public String preSubmit(Presellapply presell) {
		if(!IsNumber.isNumber(presell.getPresellApply_proposerIDCard())||"".equals(presell.getPresellApply_proposerIDCard())) {
			return"Idfalse"; 
		}else if(!IsNumber.isNumber(presell.getPresellApply_proposerPhone())||"".equals(presell.getPresellApply_proposerPhone())) {
			return"Telfalse"; 
		}else {
			
			return "success";
		}
	}
		



	@RequestMapping("/getHousesById")
	@ResponseBody
	public String getHousesById(Integer id,HttpSession session) {
		Houses houses = housesService.selectHousesById(id);
		if(houses!=null) {
			session.setAttribute("houses", houses);
			return "OK";
		}else {
			return null;
		}
	}
	@RequestMapping("/skipInterface")
	public String skipInterface(HttpSession session) {
		session.removeAttribute("houses");
		return "/houseRegister/house.jsp";

	}
}
